package com.mori.design.pattern.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 单例：懒汉式
 *
 * @author mori
 */
public class SingletonLazy  implements Serializable {

    private static SingletonLazy INSTANCE = null;

    private SingletonLazy() {
        if (INSTANCE != null) {
            throw new RuntimeException();
        }
    }

    public static synchronized SingletonLazy getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SingletonLazy();
        }
        return INSTANCE;
    }

    /** 反序列化中指定了这个方法，就会覆盖反序列化的操作，直接返回实例 */
    private Object readResolve() throws ObjectStreamException {
        return getInstance();
    }
}
